import { USER_GET_INFO, USER_SET_INFO } from '../mutation-types';
import { getUserInfoApi, changeUserInfoApi } from '../../api/';

const state = {
  user: {}
};

const getters = {
  user: (state) => state.user,
  id: (state) => state.user.id,
  nickname: (state) => state.user.nickname,
  avatar: (state) => state.user.avatar,
  realname: (state) => state.user.realname,
  gender: (state) => state.user.gender,
  birthday: (state) => state.user.birthday,
  marriage: (state) => state.user.marriage,
  children: (state) => state.user.children,
  hobby: (state) => state.user.hobby
};

const mutations = {
  [USER_GET_INFO](state, payload) {
    state.user = payload.data;
  },
  [USER_SET_INFO](state, payload) {
    state.user = payload.data;
  }
};

const actions = {
  [USER_GET_INFO]({ commit }) {
    getUserInfoApi()
      .then(res => {
        console.log(res);
        commit({
          type: USER_GET_INFO,
          data: res.data
        });
      });
  },
  [USER_SET_INFO]({ commit }, data) {
    changeUserInfoApi(data.params)
      .then(res => {
        console.log(res);
        if (data.callback) {
          data.callback(res);
        }
        commit({
          type: USER_SET_INFO,
          data: res.data
        });
      });
  }
};

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
};
